{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## kNN基础"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from matplotlib import pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "raw_data_X = [[3.393533211, 2.331273381], [3.110073483, 1.781539638], \n",
    "              [1.343808831, 3.368360954], [3.582294042, 4.679179110], \n",
    "              [2.280362439, 2.866990263], [7.423436942, 4.696522875], \n",
    "              [5.745051997, 3.533989803], [9.172168622, 2.511101045], \n",
    "              [7.792783481, 3.424088941], [7.939820817, 0.791637231]] \n",
    "raw_data_y = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]                          #肿瘤原始数据集，0良性。1恶性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "x_train = np.array(raw_data_X)   #训练集\n",
    "y_train = np.array(raw_data_y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3.39353321, 2.33127338],\n",
       "       [3.11007348, 1.78153964],\n",
       "       [1.34380883, 3.36836095],\n",
       "       [3.58229404, 4.67917911],\n",
       "       [2.28036244, 2.86699026],\n",
       "       [7.42343694, 4.69652288],\n",
       "       [5.745052  , 3.5339898 ],\n",
       "       [9.17216862, 2.51110105],\n",
       "       [7.79278348, 3.42408894],\n",
       "       [7.93982082, 0.79163723]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x_train"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_train"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.collections.PathCollection at 0x12b6b46d48>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD4CAYAAAD8Zh1EAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjMsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+AADFEAAAUgUlEQVR4nO3df4zkd33f8edrfdvA8uNc4W3j+rw7rYKiNjgBd+VALSHE0YoftmkbIhktJKBEWyHS2E0kFFgJZKT9AykipxQJNMUUU6b8qCGRD5kojoECf2C6Z2yfzSHVbb3nC269weSMs4Sc4d0/Zs7eW+/ezu7N3Xf2e8+HNJqZz/dzMy+db17+zvfHfFNVSJL2vommA0iSRsNCl6SWsNAlqSUsdElqCQtdklpiX1NvfNlll1Wn02nq7SVpTzpy5MhfVdX0ZssaK/ROp8Py8nJTby9Je1KSla2WuclFklrCQpeklrDQJaklLHRJagkLXZJawkKXpJaw0CWpJSx0SedHrwedDkxM9O97vaYTtZ6FLgB6R3t0DnWYuGWCzqEOvaN++HQOej1YWICVFajq3y8sWOrnmYUuekd7LBxeYOXkCkWxcnKFhcMLlrp2b3ER1tbOHFtb64/rvLHQxeLdi6ydOvPDt3ZqjcW7/fBpl44f39m4RsJCF8dPbv4h22pc2tbMzM7GNRIWupjZv/mHbKtxaVtLSzA1debY1FR/XOeNhS6WDi4xNXnmh29qcoqlg374tEvz89DtwuwsJP37brc/rvOmsZ/P1fiYv6r/IVu8e5HjJ48zs3+GpYNLz4xLuzI/b4FfYKmqRt54bm6u/D10SdqZJEeqam6zZW5ykaSWGLrQk1yS5DtJvrTJsnckWU1y3+D226ONKUn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Am4ONNZ9krkrwYeDVwK0BV/d04lfnAQeB/jUOZr7MPeH6SfcAU8P2G85z2T4FvVdVaVT0N/Hfg31yoN3cb+jaSdIBXAPc0m+RZg80a9wGPA3dV1bhkOwS8B/hZ00E2UcCfJzmSZKHpMOv8E2AV+M+DTVUfT/KCpkNtcCPwmaZDnFZVfwn8IXAceAw4WVV/3myqZzwIvDrJS5JMAW8ErrxQb26hn0WSFwJfAG6uqiebznNaVf20ql4OHACuGXzNa1SS64DHq+pI01m2cG1VXQ28AXj3YJPfONgHXA18tKpeAfwN8AfNRnrWYBPQDcB/azrLaUn+PvBm4B8D/wh4QZK3NZuqr6qOAR8C7qK/ueV+4OkL9f4W+hYG26e/APSq6otN59nM4Kv514DXNxwF4FrghsG26s8Cr03y6WYjPauqvj+4fxz4E/rbOMfBCeDEum9Zt9Mv+HHxBuDeqvp/TQdZ53XA/6mq1ao6BXwR+BcNZ3pGVd1aVVdX1avpb1a+INvPwULf1GDH463Asar6cNN51ksyneTSwePn0//H/b1mU0FVvbeqDlRVh/5X9K9U1VisNSV5wWDnNoPNGf+K/lfjxlXV/wUeTfKLg6GDQOM739d5K2O0uWXgOPDKJFODz+pB+vu5xkKSfzC4nwH+LRfw72/fhXqjjZJ8BngNcFmSE8AHqurWpvJscC3wduDoYFs1wPuq6s4GM512OXDb4MiDCeDzVTVWhwiOoX8I/En/s88+4L9W1Z81G+kM/x7oDTZv/G/gnQ3nAWCwDfhfAv+u6SzrVdU9SW4H7qW/OeM7jNdPAHwhyUuAU8C7q+qHF+qNPfVfklrCTS6S1BIWuiS1hIUuSS1hoUtSS1joktQSFroktYSFLkkt8f8B1bgZmZDtFDkAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(x_train[y_train==0, 0], x_train[y_train==0, 1], color=\"g\")\n",
    "plt.scatter(x_train[y_train==1, 0], x_train[y_train==1, 1], color=\"r\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.array([8.093607318, 3.365731514])    #新的数据点"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.collections.PathCollection at 0x12b6c1b888>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD4CAYAAAD8Zh1EAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjMsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+AADFEAAAUoElEQVR4nO3df4zkd33f8edrfdvA8uNc4W3j+rw7rYKiNjgBunKglhDiaMUP27QNkYwWElCirRBpTBMJBVYCGWn/QIrIKUUCbTHFlCk/akjkQyaKY6DAH5juGduHOaS6rfe44NYbTM44S8gZ3v1j5uy99ezt7N7cfWe/+3xIq5n5fD8389L55uXvfr7f70yqCknS3jfRdABJ0mhY6JLUEha6JLWEhS5JLWGhS1JLHGjqha+44orqdDpNvbwk7UnHjh37q6qaHrStsULvdDqsrKw09fKStCclWd1qm0suktQSFroktYSFLkktYaFLUktY6JLUEha6JLWEhS5JLWGhS7o4ul3odGBionfb7TadqPUsdAHQPd6lc6TDxC0TdI506B73zacL0O3CwgKsrkJV73ZhwVK/yCx00T3eZeHoAqunVymK1dOrLBxdsNS1e4uLsL5+7tj6em9cF42FLhbvXmT9zLlvvvUz6yze7ZtPu3Ty5M7GNRIWujh5evCbbKtxaVszMzsb10hY6GLm4OA32Vbj0raWlmBq6tyxqaneuC4aC10sHV5iavLcN9/U5BRLh33zaZfm52F5GWZnIendLi/3xnXRNPbxuRof89f03mSLdy9y8vRJZg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with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(x_train[y_train==0, 0], x_train[y_train==0, 1], color=\"green\")\n",
    "plt.scatter(x_train[y_train==1, 0], x_train[y_train==1, 1], color=\"red\")\n",
    "plt.scatter(x[0], x[1], color=\"blue\")       #单独绘制这个新点"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### kNN的过程"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "from math import sqrt\n",
    "distances = []\n",
    "for x_train in x_train:                        #每一个点都和预测点计算欧拉距离\n",
    "    d = sqrt(np.sum((x_train - x)**2))    \n",
    "    distances.append(d)                   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[4.812566907609877,\n",
       " 5.229270827235305,\n",
       " 6.749798999160064,\n",
       " 4.6986266144110695,\n",
       " 5.83460014556857,\n",
       " 1.4900114024329525,\n",
       " 2.354574897431513,\n",
       " 1.3761132675144652,\n",
       " 0.3064319992975,\n",
       " 2.5786840957478887]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "distances                                      #各个点距离预测点的距离"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([8, 7, 5, 6, 9, 3, 0, 1, 4, 2], dtype=int64)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.argsort(distances)                          #对距离进行 索引排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "nearest = np.argsort(distances)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 1, 1, 1, 1, 0]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "k = 6\n",
    "topk_y = [y_train[i] for i in nearest[: k]]    #距离新点最近的k个点对应的良性1，恶性0，序列\n",
    "topk_y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Counter({1: 5, 0: 1})"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from collections import Counter               #统计1 、0 个数\n",
    "Counter(topk_y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(1, 5)]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "votes = Counter(topk_y)\n",
    "votes.most_common(1)                           #votes中最多的一个元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "predict_y = votes.most_common(1)[0][0]         #取出结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "predict_y                                      #预测结果1，可能是一个恶性肿瘤"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
